﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.UI;
using YArchitech.Controls;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYModelAnalyse : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet Setelements)
		{
			Document document = commandData.Application.ActiveUIDocument.Document;
			Transaction transaction = new Transaction(document, "SpraySystemAnalyse");
			Result result;
			try
			{
				transaction.Start();
				RevitLinkInstance linkInstance = this.GetLinkInstance(document);
				string text = null;
				string text2 = null;
				string dangerLevel = null;
				string buildingType = null;
				if (linkInstance != null)
				{
					ModelAnalyser modelAnalyser = new ModelAnalyser(linkInstance.GetLinkDocument());
					text = modelAnalyser.GetBuildingVolume();
					text2 = modelAnalyser.GetBuildingHeight();
					dangerLevel = modelAnalyser.GetDangerLevel();
					buildingType = modelAnalyser.GetBuildingType();
				}
				if (text == null && text2 == null)
				{
					ModelAnalyser modelAnalyser2 = new ModelAnalyser(document);
					text = modelAnalyser2.GetBuildingVolume();
					text2 = modelAnalyser2.GetBuildingHeight();
					dangerLevel = modelAnalyser2.GetDangerLevel();
					buildingType = modelAnalyser2.GetBuildingType();
				}
				FormModelAnalyse formModelAnalyse = new FormModelAnalyse(text, text2, buildingType, dangerLevel);
				formModelAnalyse.ShowDialog();
				if (formModelAnalyse.DialogResult != DialogResult.OK)
				{
					formModelAnalyse.Dispose();
					transaction.RollBack();
					transaction.Dispose();
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					ProjectInfoModel pim = new ProjectInfoModel(formModelAnalyse.buildingVolume, formModelAnalyse.buildingHeight, formModelAnalyse.buildingType, formModelAnalyse.dangerLevel);
					this.AddProjectInfo(pim, document);
					transaction.Commit();
					transaction.Dispose();
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				transaction.RollBack();
				transaction.Dispose();
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		private RevitLinkInstance GetLinkInstance(Autodesk.Revit.DB.Document doc)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(RevitLinkInstance));
			foreach (Element element in filteredElementCollector)
			{
				RevitLinkInstance revitLinkInstance = element as RevitLinkInstance;
				if (this.IsDocHasWalls(revitLinkInstance.Document))
				{
					return revitLinkInstance;
				}
			}
			return null;
		}

		private bool IsDocHasWalls(Autodesk.Revit.DB.Document doc)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(Wall));
			return filteredElementCollector.ToElements().Count > 1;
		}

		private bool AddProjectInfo(ProjectInfoModel pim, Autodesk.Revit.DB.Document doc)
		{
			ProjectInfo projectInformation = doc.ProjectInformation;
			bool flag = false;
			Schema schema = null;
			foreach (Guid guid in projectInformation.GetEntitySchemaGuids())
			{
				Schema schema2 = Schema.Lookup(guid);
				if (schema2.SchemaName == "BuildingInfo")
				{
					flag = true;
					schema = schema2;
					break;
				}
			}
			if (!flag)
			{
				try
				{
					SchemaBuilder schemaBuilder = new SchemaBuilder(Guid.NewGuid());
					schemaBuilder.SetReadAccessLevel((AccessLevel)1);
					schemaBuilder.SetWriteAccessLevel((AccessLevel)1);
					schemaBuilder.SetVendorId("ADSK");
					schemaBuilder.SetSchemaName("BuildingInfo");
					schemaBuilder.AddSimpleField("BuildingVolume", typeof(string)).SetDocumentation("建筑物体积");
					schemaBuilder.AddSimpleField("BuildingHeight", typeof(string)).SetDocumentation("建筑物高度");
					schemaBuilder.AddSimpleField("BuildingType", typeof(string)).SetDocumentation("建筑类型");
					schemaBuilder.AddSimpleField("DangerLevel", typeof(string)).SetDocumentation("危险等级");
					Schema schema3 = schemaBuilder.Finish();
					Entity entity = new Entity(schema3);
					Field field = schema3.GetField("BuildingVolume");
					entity.Set<string>(field, pim.BuildingVolume.ToString(), (Autodesk.Revit.DB.DisplayUnitType)0);
					field = schema3.GetField("BuildingHeight");
					entity.Set<string>(field, pim.BuildingHeight.ToString(), (Autodesk.Revit.DB.DisplayUnitType)0);
					field = schema3.GetField("BuildingType");
					entity.Set<string>(field, pim.BuildingType, (Autodesk.Revit.DB.DisplayUnitType)0);
					field = schema3.GetField("DangerLevel");
					entity.Set<string>(field, pim.DangerLevel, (Autodesk.Revit.DB.DisplayUnitType)0);
					projectInformation.SetEntity(entity);
					return true;
				}
				catch (Exception ex)
				{
					YJKMessageBox.Show(ex.Message);
					return true;
				}
			}
			Entity entity2 = new Entity(schema);
			Field field2 = schema.GetField("BuildingVolume");
			entity2.Set<string>(field2, pim.BuildingVolume.ToString(), 0);
			field2 = schema.GetField("BuildingHeight");
			entity2.Set<string>(field2, pim.BuildingHeight.ToString(), 0);
			field2 = schema.GetField("BuildingType");
			entity2.Set<string>(field2, pim.BuildingType, 0);
			field2 = schema.GetField("DangerLevel");
			entity2.Set<string>(field2, pim.DangerLevel, 0);
			projectInformation.SetEntity(entity2);
			return true;
		}
	}
}
